const express = require('express')
const router = express.Router();
const Warning = require('../controllers/warning')
const Socket = require('../index')
const Setting = require('../setting')


router.get('/getAll', ((req, res) => {
  Warning.findAll({},(data) => {
        res.send({code: Setting.Success, data: data})
    })
}))

router.post('/add', ((req, res) => {
    const {number, deviceNumber, type, time, state} = req.body
    if(!number || !time || !deviceNumber || !type || !state) {
        res.send({code: Setting.MissParameter, message: '缺失参数'})
    }else {
        Warning.add({number, deviceNumber, type, time, state}, (data) => {
            res.send({code: Setting.Success, data: data})
            Socket.emit(data);
        })
    }
}))

router.get('/getAllUnhandled', ((req, res) => {
    Warning.findByConditions({state: '0'}, (data) => {
      res.send({code: Setting.Success, data: data})
    })
}))
module.exports = router;
